package com.ruoyi.teacher.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.teacher.domain.TEssayAuthor;
import com.ruoyi.teacher.mapper.TEssayMapper;
import com.ruoyi.teacher.domain.TEssay;
import com.ruoyi.teacher.service.ITEssayService;

/**
 * 论文信息集Service业务层处理
 *
 * @author xiangzy
 * @date 2022-05-10
 */
@Service
public class TEssayServiceImpl implements ITEssayService
{
    @Autowired
    private TEssayMapper tEssayMapper;

    /**
     * 查询论文信息集
     *
     * @param recordid 论文信息集ID
     * @return 论文信息集
     */
    @Override
    public TEssay selectTEssayById(Long recordid)
    {
        return tEssayMapper.selectTEssayById(recordid);
    }

    /**
     * 查询论文信息集列表
     *
     * @param tEssay 论文信息集
     * @return 论文信息集
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TEssay> selectTEssayList(TEssay tEssay)
    {
        return tEssayMapper.selectTEssayList(tEssay);
    }

    /**
     * 通过A00查询论文信息集列表
     *
     * @param A00 论文信息集
     * @return 论文信息集
     */
    @Override
    public List<TEssay> selectTEssayListByA00(Long A00)
    {
        return tEssayMapper.selectTEssayListByA00(A00);
    }

    /**
     * 新增论文信息集
     *
     * @param tEssay 论文信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTEssay(TEssay tEssay)
    {
        tEssay.setCreateTime(DateUtils.getNowDate());
        int rows = tEssayMapper.insertTEssay(tEssay);
        insertTEssayAuthor(tEssay);
        return rows;
    }

    /**
     * 修改论文信息集
     *
     * @param tEssay 论文信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTEssay(TEssay tEssay)
    {
        tEssay.setUpdateTime(DateUtils.getNowDate());
        tEssayMapper.deleteTEssayAuthorByEASSYID(tEssay.getRecordid());
        insertTEssayAuthor(tEssay);
        return tEssayMapper.updateTEssay(tEssay);
    }

    /**
     * 批量删除论文信息集
     *
     * @param recordids 需要删除的论文信息集ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTEssayByIds(Long[] recordids)
    {
        tEssayMapper.deleteTEssayAuthorByEASSYIDs(recordids);
        return tEssayMapper.deleteTEssayByIds(recordids);
    }

    /**
     * 删除论文信息集信息
     *
     * @param recordid 论文信息集ID
     * @return 结果
     */
    @Override
    public int deleteTEssayById(Long recordid)
    {
        tEssayMapper.deleteTEssayAuthorByEASSYID(recordid);
        return tEssayMapper.deleteTEssayById(recordid);
    }

    /**
     * 新增论文作者信息集信息
     *
     * @param tEssay 论文信息集对象
     */
    public void insertTEssayAuthor(TEssay tEssay)
    {
        List<TEssayAuthor> tEssayAuthorList = tEssay.getTEssayAuthorList();
        Long recordid = tEssay.getRecordid();
        if (StringUtils.isNotNull(tEssayAuthorList))
        {
            List<TEssayAuthor> list = new ArrayList<TEssayAuthor>();
            for (TEssayAuthor tEssayAuthor : tEssayAuthorList)
            {
                tEssayAuthor.setEassyid(recordid);
                list.add(tEssayAuthor);
            }
            if (list.size() > 0)
            {
                tEssayMapper.batchTEssayAuthor(list);
            }
        }
    }
}
